package com.tulele.vtool.task.process.articlePush;

import com.tulele.vtool.bus.mapper.log.PushDetailLogMapper;
import com.tulele.vtool.bus.mapper.log.PushHeadLogMapper;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.log.PushHeadLog;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.task.async.DailyPaperTempMsgAsyncTask;
import com.tulele.vtool.task.async.FifthTimeArticlePushAsyncTask;
import com.tulele.vtool.task.async.UserDailyBrandSetInfoPushAsyncTask;
import com.tulele.vtool.util.SpringContextUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class DailyPaperTempMsgPushTask extends QuartzJobBean {

    @Autowired
    private DailyPaperTempMsgAsyncTask dailyPaperTempMsgAsyncTask;

    @Autowired
    private UserDailyBrandSetInfoPushAsyncTask userDailyBrandSetInfoPushAsyncTask;

    @Autowired
    private IUserService userService;

    @Value("${sleepTime}")
    private String sleepTimeSet;

    @Autowired
    private PushHeadLogMapper pushHeadLogMapper;

    @Autowired
    private PushDetailLogMapper pushDetailLogMapper;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Value("${spring.application.name}")
    private String springProfilesActive;

    @SneakyThrows
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Integer sleepTime = Integer.valueOf(sleepTimeSet) * 1000 * 60;
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String profiles_str = springProfilesActive;//获取环境
        String[] profile_arr = profiles_str.split(",");
        List<Integer> headIds = new ArrayList<>();
        for (String profile : profile_arr) {

            PushHeadLog pushHeadLog = new PushHeadLog();
            pushHeadLog.setPushName("日报");
            pushHeadLog.setMsgType(9);
            pushHeadLog.setMsgTypeName("日报");
            pushHeadLog.setSumCount(0);
            pushHeadLog.setCreateTime(new Date());
            pushHeadLog.setGzhCode(profile);

            pushHeadLogMapper.addPushHeadLog(pushHeadLog);
            Integer headId = pushHeadLog.getId();

            //List<UserInfo> userList = userService.getUserListByGzhCodeOut(profile);

            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (week == 4) { //周四发品牌
                List<UserInfo> userList = userService.getAllUserListByGzhCode(profile);
                for (UserInfo userInfo : userList) {
//                    userDailyBrandSetInfoPushAsyncTask.execute(userInfo,profile);
                }
            } else {
                WeixinServiceDL weixinService= WeixinServiceMap.getWeixinService(profile);
                if(weixinService==null) {
                    weixinService = objectFactory.getObject();
                    weixinService.reSetConfigStorage(profile);
                    WeixinServiceMap.putWeixinService(profile,weixinService);
                }

                WxMpUserList userList = weixinService.getUserService().userList(null);
                long total = userList.getTotal();

                do {
                    for (String openId : userList.getOpenids()) {
                        try {
                            dailyPaperTempMsgAsyncTask.execute(openId, headId,profile);
                        } catch (Exception e) {

                        }
                    }
                    if (userList.getNextOpenid() != null && !userList.getNextOpenid().isEmpty()) {
                        userList = weixinService.getUserService().userList(userList.getNextOpenid());
                    } else {
                        userList = null;
                    }
                } while (userList != null && userList.getCount() > 0);
            }

            headIds.add(headId);
        }

        Thread.sleep(1000 * 60 * 30);

        for (Integer headId : headIds) {
            PushHeadLog pushHeadLogU = new PushHeadLog();
            pushHeadLogU.setId(headId);
            pushHeadLogU.setSuccessCount(pushDetailLogMapper.getSuccessCount(headId));
            pushHeadLogU.setErrCount(pushDetailLogMapper.getErrCount(headId));
            pushHeadLogU.setUpdateTime(new Date());
            pushHeadLogU.setEndTime(new Date());

            pushHeadLogMapper.updatePushHeadLog(pushHeadLogU);
        }

    }
}
